package com.hos.db.manager;


import com.hos.db.create.msg.MsgTable;
import com.hos.db.entity.msg.MsgEntity;
import com.hos.utile.DLog;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.ArrayList;
import java.util.List;

//消息
public class MsgManager {

    private static MsgManager userPatsManager;

    public static MsgManager getInstance() {
        if (userPatsManager == null) {
            userPatsManager = new MsgManager();
        }
        return userPatsManager;
    }

    /**
     * 获取系统消息列表
     *
     * @param jdbcTemplate
     * @param patId        患者id
     * @param pageNo       页面（>=1）
     * @param pageSize     每页查询数量
     * @param hasNextPage  判断是否有下一页
     */
    public List<MsgEntity> onMsgsReq(JdbcTemplate jdbcTemplate, String docId,
                                     String patId, int pageNo, int pageSize,
                                     boolean hasNextPage) {
        //SELECT column_name,column_name FROM table_name [WHERE Clause] [LIMIT N][ OFFSET M]
        int next = 0;
        if (hasNextPage) {
            next = 1;
        }
        String id = docId;
        if (id == null || id == "") {
            id = patId;
        }
        String w = " WHERE "
                + MsgTable.tableMsgOwnerId + " = '" + id + "'";
        String sql = "SELECT * FROM " + MsgTable.tableName
                + w
                + " ORDER BY " + MsgTable.tableMsgCreateTime
                + " DESC "
                + " limit "
                + (pageNo - 1) * pageSize + " , " + (pageSize + next);

        List<MsgEntity> list = onQuery(jdbcTemplate, sql);
        if (list == null) {
            list = new ArrayList();
        }
        return list;
    }

    //查询访问
    private List<MsgEntity> onQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<MsgEntity> list = null;
        try {
            list = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(MsgEntity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return list;
    }
}
